package com.lzx.applib.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;

import java.util.ArrayList;
import java.util.List;

public abstract class SimpleAdapter<T> extends BAdapter {

	protected List<T> dataList;
	protected Context context;
	protected LayoutInflater inflater;
	
	public SimpleAdapter(Context context, List<T> list) {
		this.context = context;
		this.dataList = list;
		this.inflater = LayoutInflater.from(context);

		if (dataList == null) {
			dataList = new ArrayList<>();
		}
	}
	
	public void add(int position, T data) {
		dataList.add(position, data);
		notifyDataSetChanged();
	}
	
	public void add(List<T> addList) {
		dataList.addAll(addList);
		notifyDataSetChanged();
	}
	
	public void add(T data) {
		dataList.add(data);
		notifyDataSetChanged();
	}
	
	public void addToTop(List<T> addList) {
		dataList.clear();
		if (addList == null) {
			notifyDataSetChanged();
		} else {
			add(addList);
		}
	}
	
	public void addToTop(T data) {
		dataList.clear();
		add(data);
	}
	
	public void remove(T t) {
		if (dataList.contains(t)) {
			dataList.remove(t);
			notifyDataSetChanged();
		}
	}
	
	public void remove(int index) {
		if (0<=index && index<=dataList.size()) {
			remove(dataList.get(index));
		}
	}
	
	public void removeAll() {
		dataList.clear();
		notifyDataSetChanged();
	}
	
	@Override
	public T getItem(int position) {
		return dataList.get(position);
	}
	
	@Override
	public int getItemCount() {
		return dataList.size();
	}
	
	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		((SimpleViewHolder)holder).init(getItem(position), position);
	}
}
